package com.iexbuy.ihg.ui.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.iexbuy.ihg.ui.activity.BaseActivity;


/**
 * @author ban
 *
 */
public abstract class BaseFragment extends Fragment {

	protected BaseActivity mBaseActivity;
	
	protected final OnClickListener mLoginClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			//startActivityForResult(new Intent(mBaseActivity, LoginActivity.class), 0);
		}
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mBaseActivity = (BaseActivity) getActivity();
		mBaseActivity.setHomeActivity(false);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mBaseActivity.setSearchButtonVisibility(View.GONE, null);
		return _onCreateView(inflater, container, savedInstanceState);
	}

	protected abstract View _onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
	
	
	/**
	 * @param context
	 * @param fragmentContainer
	 */
	public void startFragment(FragmentActivity context, int fragmentContainer) {
		FragmentManager fragmentManager = context.getSupportFragmentManager();
		fragmentManager.popBackStack();
		FragmentTransaction ft = fragmentManager.beginTransaction();
		ft.replace(fragmentContainer, this);
		ft.addToBackStack(null);
		ft.commit();
	}
	
	public void startFragment(FragmentActivity context, int fragmentContainer, Fragment toFragment) {
		FragmentManager fragmentManager = context.getSupportFragmentManager();
		fragmentManager.popBackStack();//在那个切换Fragment的事务开启之前，把原来的事务POP掉::::::::::
		FragmentTransaction ft = fragmentManager.beginTransaction();
//		ft.setCustomAnimations(R.anim.push_left_in,R.anim.push_left_in,R.anim.back_left_in,R.anim.back_right_out);   
//		ft.replace(fragmentContainer, toFragment);
		ft.replace(getId(), toFragment);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
		ft.addToBackStack(null);
		ft.commit();
	}

	public int finish(FragmentActivity context) {
		FragmentManager fragmentManager = context.getSupportFragmentManager();
		if (fragmentManager.getBackStackEntryCount() > 1) {
			fragmentManager.getFragments().get(fragmentManager.getBackStackEntryCount()-2).onResume();
			fragmentManager.popBackStack();
			
		}
		
		return fragmentManager.getBackStackEntryCount();
	}
	

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}

	public void onStart() {
		Log.i(getClass().getSimpleName(), "life onStart()");
		super.onStart();
		mBaseActivity.setCurrentFragment(this);
		mBaseActivity.setHomeActivity(true);
	}

	@Override
	public void onResume() {
		Log.i(getClass().getSimpleName(), "life onResume()");
		super.onResume();
	}

	@Override
	public void onPause() {
		Log.i(getClass().getSimpleName(), "life onPause()");
		super.onPause();
	}

	public void onStop() {
		Log.i(getClass().getSimpleName(), "life onStop()");
		super.onStop();
		mBaseActivity.closeDialog();
	}
	
	
	
	public void cancelTask(AsyncTask task){
		if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
			task.cancel(true);
		}
	}
	
}
